package com.example.leonard.wapper;

import com.example.leonard.common.enums.CodeMsg;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.poi.ss.formula.functions.T;

/**
 * @auther heyang
 * @date 2021/8/29 17:48
 */
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class ResultWrapper<T> {
    public static final int SUCCSE_CODE = 200;
    public static final int ERROR_CODE = 500;
    public static final String ERROR_MSG = "登陆失败,系统出现异常,请稍后重试!";

    private int code; //错误码
    private String msg; //错误信息  200表示成功 否则失败
    private T data; //返回的错误信息

    //成功
    public static <T> ResultWrapper<T> success(T date) {
        return new ResultWrapper<>(SUCCSE_CODE, null, date);
    }

    //其他错误
    public static <T> ResultWrapper<T> defaultError() {
        return new ResultWrapper<>(ERROR_CODE, ERROR_MSG, null);
    }

    //自定义错误
    public static <T> ResultWrapper<T> error(CodeMsg codeMsg) {
        return new ResultWrapper<>(codeMsg.getCode(), codeMsg.getMsg(), null);
    }

    public Boolean hasError() {
        return this.getCode() != 200;
    }
}
